<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        实现一个函数，传入一个对象，能够返还一个新的对象内容和旧对象内容一样<br>
        var obj = {<br>
            name:"张三",<br>
            obj1:{<br>
                myname:"李四",<br>
                age:20,<br>
            }<br>
        }<br>
    </div>
    <script>
        function copyObj ( obj ) { //创建一个函数，具有一个形参
            var obj1 = {} ;     //定义一个没有键值对的对象
            for( var key in obj ) { //传过来的对象中进行循环
                var i = key ;       //声明一个变量i，将对象的键名赋值给这个变量
                obj1[i] = obj[key] ;//在新对象中相同的位置将键值赋值给新对象对应的键名
            }
            obj.obj1 = obj1 ;
            return obj ;
        }
        function copyObject ( obj ) { //深度拷贝
            var obj1 = {} ; 
            for( var key in obj ) {
                if (typeof obj[key] == 'object' ){
                    var obj2 = {} ;
                    for ( var i in obj[key] )
                    {
                        obj2[i] = obj[key][i] ;
                    }
                    obj1[key] = obj2 ;
                }
                obj1[key] = obj[key] ;
            }
            return obj1 ;
        }
        var stuInfo = {         
            id : 2113001,      
            idCard: '42092319960714635X' ,
            age: 25
        } ;
        var person = copyObj(stuInfo);
        console.log(person);
        console.log(copyObject(person));
    </script>
</body>
</html>